package com.competition.front.controller;
import com.competition.commonutils.JwtUtils;
import com.competition.commonutils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.competition.front.entity.Module;
import com.competition.front.entity.ModuleRelationship;
import com.competition.front.service.ModuleRelationshipService;
import com.competition.front.service.ModuleService;
import com.competition.front.service.UserAdditionalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
 * <p>
 * 投稿信息表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/front/module")
@CrossOrigin
@Api(description = "模块相关操作的接口")
public class ModuleController {

    @Autowired
    private ModuleRelationshipService moduleRelationshipService;

    @Autowired
    private UserAdditionalService userAdditionalService;

    @Autowired
    private ModuleService moduleService;

    @ApiOperation("获取全部模块")
    @GetMapping("getModuleRelationship")
    public R getModuleRelationship(){
        List<ModuleRelationship> moduleRelationshipList = moduleRelationshipService.list(null);
        return R.ok().data("moduleRelationshipList",moduleRelationshipList);
    }

    @ApiOperation("根据模块id获取对应模块=>1：认识病毒， 2：抗议故事， 3：疫情新况")
    @GetMapping("getModuleById/{moduleId}/{page}/{limit}")
    public R  selectModuleById(@PathVariable("moduleId") String moduleId,
                                    @PathVariable("page") Integer page,
                                    @PathVariable("limit") Integer limit){
        List<Module> list=moduleService.selectModuleById(moduleId,page,limit);
        Integer count=moduleService.countModuleByRelationId(moduleId);
        return R.ok().data("modules",list).data("total",count);
    }

    @ApiOperation("根据用户id查看对应稿子")
    @PostMapping("selectModuleByUserId")
    public  R  selectModuleByUserId(@RequestParam String userId,@RequestParam String relationId){
        List<Module> list=moduleService.selectModuleByUserIdAndRelationId(userId,relationId);
        QueryWrapper<Module> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("relationship_id",relationId);
        queryWrapper.eq("","0");
        return R.ok().data("modules",list).data("total",moduleService.count(queryWrapper));
    }

    @ApiOperation("用户投稿")
    @PostMapping("addModule")
    public R addModule(@RequestBody Module module, HttpServletRequest request){
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        module.setUserId(userId);
        boolean save = moduleService.saveModule(module);
        return save==true?R.ok().data("module",module):R.error();
    }
    @ApiOperation("删除稿子，根据模块id")
    @DeleteMapping("deleteModule/{id}")
    public R deleteModule(@PathVariable String id, HttpServletRequest request){
        boolean flag = moduleService.removeById(id);
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        userAdditionalService.reduceEssayNumber(memberIdByJwtToken);
        return flag==true?R.ok():R.error();
    }
}

